package me.kagami.exex.intercepter;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;

public class PlaceholderIntercepter extends InsertDataIntercepter {
	private String placeholder = "";
	private Map<String, String> dataMap = new HashMap<>();

	public Map<String, String> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, String> dataMap) {
		this.dataMap = dataMap;
	}

	@Override
	public <T> void beforeInsertData(Cell cell, T data, int rowNumber, int colNumber) {
		placeholder = cell.getStringCellValue();
	}

	@Override
	public <T> void afterInsertData(Cell cell, T data, int rowNumber, int colNumber) {
		if (StringUtils.isEmpty(placeholder)) {
			return;
		}
		String dataStr = dataMap.get(placeholder.substring(2, placeholder.length() - 1));
		if (StringUtils.isNotEmpty(dataStr)) {
			try {
				cell.setCellValue(Double.parseDouble(dataStr));
			} catch (Exception e) {
				cell.setCellValue(dataStr);
			}
		}
	}

}
